using System;
using System.Diagnostics;
using System.IO;

using OpenXmlWriter.WordprocessingML.Packaging;

namespace OpenXmlWriter.Sample
{
  internal class Program
  {
    [STAThread]
    private static void Main()
    {
      CreateWordDoc();
    }

    private static void CreateWordDoc()
    {
      using (
        WordprocessingDocumentWriter writer =
          WordprocessingDocumentWriter.Create("test.docx", WordprocessingDocumentType.Document))
      {
        MainDocumentPart mainPart = writer.AddMainDocumentPart();
        ImagePart imagePart = mainPart.AddImagePart("preview.gif", "image/gif");
        HeaderPart headerPart = mainPart.AddHeaderPart();
        FooterPart footerPart = mainPart.AddFooterPart();
        StyleDefinitionsPart styleDefinitionsPart = mainPart.AddStyleDefinitionsPart();
        NumberingDefinitionsPart numberingDefinitionsPart = mainPart.AddNumberingDefinitionsPart();
        numberingDefinitionsPart.WriteContent(GetNumberingXml());
        styleDefinitionsPart.WriteContent(GetStyleXml());
        headerPart.WriteContent(GetHeaderXml());
        footerPart.WriteContent(GetFooterXml());
        imagePart.WriteContent(File.Open(@"..\..\..\lib\PREVIEW.GIF", FileMode.Open));
        mainPart.WriteContent(GetDocumentXml(headerPart, footerPart, imagePart));
      }
      ProcessStartInfo psi = new ProcessStartInfo("test.docx");
      psi.WindowStyle = ProcessWindowStyle.Normal;
      Process.Start(psi);
    }

    private static string GetNumberingXml()
    {
      return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
             "<w:numbering xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +
             "<w:abstractNum w:abstractNumId=\"1\">" +
             "<w:multiLevelType w:val=\"singleLevel\"/>" +
             "<w:lvl w:ilvl=\"0\">" +
             "<w:numFmt w:val=\"bullet\"/>" +
             "<w:pStyle w:val=\"Calculation\"/>" +
             "<w:lvlText w:val=\"&gt;\"/>" +
             "<w:lvlJc w:val=\"left\"/>" +
             "<w:pPr>" +
             "<w:ind w:left=\"360\" w:hanging=\"360\"/>" +
             "</w:pPr>" +
             "<w:rPr>" +
             "<w:rFonts w:ascii=\"Courier New\" w:hAnsi=\"Courier New\" w:hint=\"default\"/>" +
             "</w:rPr>" +
             "</w:lvl>" +
             "</w:abstractNum>" +
             "<w:num><w:abstractNumId w:val=\"1\" /></w:num>" +
             "</w:numbering>";
    }

    private static string GetStyleXml()
    {
      return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
             "<w:styles xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +
             "<w:docDefaults />" +
             "<w:lantentStyles />" +
             "<w:style w:type=\"paragraph\" w:styleId=\"Kahuna\">" +
             "<w:name w:val=\"Kahuna\" />" +
             "<w:next w:val=\"Normal\" />" +
             "<w:basedOn w:val=\"Normal\" />" +
             "<w:qFormat />" +
             "<w:rPr>" +
             "<w:rFonts w:ascii=\"Impact\" />" +
             "<w:spacing w:val=\"5\" />" +
             "<w:sz w:val=\"128\" />" +
             "</w:rPr>" +
             "</w:style>" +
             "<w:style w:type=\"paragraph\" w:styleId=\"Notes\">" +
             "<w:name w:val=\"Notes\" />" +
             "<w:next w:val=\"Normal\" />" +
             "<w:basedOn w:val=\"Normal\" />" +
             "<w:qFormat />" +
             "<w:pPr><w:ind w:left=\"144\" w:hanging=\"144\" /></w:pPr>" +
             "<w:rPr>" +
             "<w:rFonts w:ascii=\"Tahoma\" />" +
             "<w:i />" +
             "</w:rPr>" +
             "</w:style>" +
             "<w:style w:type=\"paragraph\" w:styleId=\"Calculation\">" +
             "<w:name w:val=\"Calculation\" />" +
             "<w:next w:val=\"Normal\" />" +
             "<w:basedOn w:val=\"Normal\" />" +
             "<w:qFormat />" +
             "<w:pPr>" +
             "<w:numPr><w:numId w:val=\"1\" /></w:numPr>" +
             "<w:spacing w:before=\"0\" w:beforeAutospacing=\"0\" w:after=\"0\" w:afterAutospacing=\"0\" w:line=\"240\" w:lineRule=\"auto\" />" +
             "<w:ind w:left=\"144\" w:hanging=\"144\" />" +
             "</w:pPr>" +
             "<w:rPr>" +
             "<w:rFonts w:ascii=\"Courier New\" />" +
             "</w:rPr>" +
             "</w:style>" +
             "</w:styles>";
    }

    private static string GetDocumentXml(HeaderPart headerPart, FooterPart footerPart, ImagePart imagePart)
    {
      return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
             "<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
             "xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
             "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">" +
             "<w:body>" +
             "<w:p><w:pPr><w:pStyle w:val=\"Kahuna\" /></w:pPr><w:r><w:t>Hello World!</w:t></w:r></w:p>" +
             "<w:p><w:pPr><w:pStyle w:val=\"Calculation\" /></w:pPr><w:r><w:t>Calculation Item 1</w:t></w:r></w:p>" +
             "<w:p><w:pPr><w:pStyle w:val=\"Calculation\" /></w:pPr><w:r><w:t>Calculation Item 2</w:t></w:r></w:p>" +
             "<w:p><w:pPr><w:pStyle w:val=\"Calculation\" /></w:pPr><w:r><w:t>Calculation Item 3</w:t></w:r></w:p>" +
             "<w:p><w:pPr><w:pStyle w:val=\"Calculation\" /></w:pPr><w:r><w:t>Calculation Item 4</w:t></w:r></w:p>" +
             "<w:p><w:r><w:pict>" +
             "<v:shape id=\"myShape1\" type=\"#_x0000_t75\" style=\"width:400;height:240\"><v:imagedata r:id=\""
             + imagePart.Relationship.Id + "\" /></v:shape>" +
             "</w:pict></w:r></w:p>" +
             "<w:sectPr>" +
             "<w:headerReference w:type=\"default\" r:id=\"" + headerPart.Relationship.Id + "\" />" +
             "<w:footerReference w:type=\"default\" r:id=\"" + footerPart.Relationship.Id + "\" />" +
             "<w:pgSz w:w=\"12240\" w:h=\"15840\" />" +
             "<w:pgMar w:top=\"1440\" w:right=\"1440\" w:bottom=\"1440\" w:left=\"1440\" w:header=\"720\" w:footer=\"720\" w:gutter=\"0\" />" +
             "<w:cols w:space=\"720\" />" +
             "</w:sectPr>" +
             "</w:body>" +
             "</w:document>";
    }

    private static string GetHeaderXml()
    {
      return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
             "<w:hdr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +
             "<w:p>" +
             "<w:pPr><w:jc w:val=\"center\" /></w:pPr>" +
             "<w:r><w:t>This is the header</w:t></w:r>" +
             "</w:p>" +
             "</w:hdr>";
    }

    private static string GetFooterXml()
    {
      return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
             "<w:ftr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +
             "<w:p>" +
             "<w:pPr><w:jc w:val=\"center\" /></w:pPr>" +
             "<w:r><w:t>This is the footer</w:t></w:r>" +
             "</w:p>" +
             "</w:ftr>";
    }
  }
}